package com.mojo.mybatis;

import com.mojo.mybatis.dao.BackstageUserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Mybatis执行流程
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2023, 广州九尾信息科技有限公司
 * @date 2023/4/6 20:38
 */
public class MybatisTheory {
    public static void main(String[] args) throws IOException {
        /**mybatis的执行流程*/
        // 1、创建SqlSessionFactory
        String resource = "org/mybatis/example/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 2、创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BackstageUserDao mapper = sqlSession.getMapper(BackstageUserDao.class);
        // 3、获取Mapper,执行SQL语句
        mapper.select(1);
        // 4、增删改需要提交事务
        // sqlSession.commit();
    }
}
